import axios from "axios";
import {ElMessage} from "element-plus";

const request = axios.create({
    baseURL: '/api',
    timeout: 5000
})

request.interceptors.request.use(
    (config) => {
        config.headers['token'] = ''
        return config
    }
)

request.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        let status = error.response.status
        switch (status) {
            case 404:
                ElMessage({
                    type: 'error',
                    messsage: error.messsage
                })
                break;
            case 500 | 501 | 502 | 503 | 504 | 505:
                ElMessage({
                    type: 'error',
                    messsage: '服务器无法访问'
                })
                break;
        }
        return Promise.reject(new Error(error.message))
    }
)

export default request
